<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    :visible.sync="visible"
    width="800px"
    append-to-body
  >
    <el-form ref="form" :model="form" label-width="auto" :rules="rules">      

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="药企名称" prop="name" class="person">
            <el-input
              v-model="form.name"
              placeholder="药企名称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="选择医生" prop="user" class="person">
            <el-select v-model="form.userList" filterable multiple placeholder="请选择">
              <el-option
                v-for="item in doctors"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>

          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>

  </el-dialog>
</template>

<script>
import BindDialog from '@/components/bindDialog/index.vue'
import { dialogFrame } from './frameData'
import { modifyEnterprises, addEnterprises } from '@/api/enterprises'
import { getDoctorList } from '@/api/doctor';

const markForm = () => {
  return {
    name: "",
    userList: []
  }
}

export default {
  name: "",
  components: {
    BindDialog
  },
  data() {
    return {
      rules: {},
      title: "",
      type: "",
      config: {
        edit: {
          func: modifyEnterprises,
          title: "编辑"
        },
        new: {
          func: addEnterprises,
          title: "新增"
        }
      },
      form: markForm(),
      visible: false,
      inputValue: "",
      inputVisible: false,
      dialogFrame,
      query: '',
      loading: false,
      doctors: []
    };
  },
  mounted() {
    this.queryDoctorAsync()
  },
  methods: {
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput?.$refs?.input.focus();
      });
    },

    queryDoctorAsync() {
      getDoctorList({
          pageNum: 1,
          pageSize: 500,
      }).then(res => {
        if(res.rows && res.rows.length >= 0){
          this.doctors = res.rows.filter(item => item.name).map(item => {
            return {
              label: item.name,
              value: item.id
            }
          })
        }
      })
    },

    showDialog(row) {
      this.reset();
      const isEdit = !!row;
      this.visible = true;
      this.type = isEdit ? "edit" : "new";
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
      if (isEdit) {
        const userList = row.userList.map(item => {
          return item.id
        })
        this.form = Object.assign(this.form, row, {
          userList
        });

      }
      this.title = this.config[this.type].title;
    },

    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.visible = false;
          const user = this.doctors.filter(item => {
            return this.form.userList.includes(item.value)
          }).map(item => {
            return {
              id: item.value,
              name: item.label
            }
          })

          const data = { ...this.form, userList: user }

          if(this.type === "new"){
            delete data.id;
          }else{
            delete data.userNum
            delete data.num
            delete data.createTime
          }

          this.config[this.type]?.func(data).then(() => {
            this.$modal.msgSuccess("提交成功");
            this.visible = false;
            this.$emit("getTableList");
          });
        } else {
          return false;
        }
      });
    },

    cancel() {
      this.visible = false;
    },

    reset() {
      this.form = markForm();
      this.$forceUpdate();
    },

  },
};
</script>
<style scoped lang="scss">
.label-list{
  height: 36px;
  display: flex;
  align-items: center;

  .el-tag{
    margin-right: 10px;
  }

  .button-new-tag{
    height: 28px;
    padding: 0 20px;
  }

  .input-new-tag{
    width: 100px;
    height: 28px;
    display: flex;

    ::v-deep .el-input__inner {
      width: 100%;
      height: 100%;
    }
  }
}
.person{
  position: relative;

  .el-icon-user{
    position: absolute;
    top: 10px;
    right: 10px;
    cursor: pointer;
  }

  .cover{
    width: 100%;
    height: 100%;
    position:absolute;
    top: 0;
    left: 0;
    z-index: 1;
    cursor: pointer;

    &:hover {
      .el-input__inner{
        border-color: #C0C4CC;
      }
    }
  }
}
</style>
